import re

from database.database_controllor import DatabaseControllor
from common import config
from common import datasetInfo

class Module():
    def __init__(self, dataset):
    #dataset should be the file name of the dataset
        self.table_name = dataset
        self.attribute_list = getattr(datasetInfo, 'attribute_list')[self.table_name]
        con = DatabaseControllor()
        con.connect()
        self.table = con.select(self.attribute_list, self.table_name)
        con.disconnect()
        self.attributes_info = []
    
    def fetchTable(self):
        return self.table

    def getAttributeList(self):
        return self.attribute_list

    def getTableName(self):
        return self.table_name

    def getAttributeInfo(self):
        attributesInfoList = getattr(datasetInfo, 'attributes')['attributeList']
        for item in attributesInfoList:
            if item["table"] == self.table_name:
                self.attributes_info = item["attributes"]

        continuous_attribute_list = []
        for i in range(0, len(self.attributes_info)):
            self.attributes_info[i]["ignore"] = False
            self.attributes_info[i]["discretized"] = -1
            if re.search(r'integer|numeric', self.attributes_info[i]["type"]):
                self.attributes_info[i]["max"] = self.table[0][i]
                self.attributes_info[i]["min"] = self.table[0][i]
                continuous_attribute_list.append(i)
            else:
                self.attributes_info[i]["max"] = 'None'
                self.attributes_info[i]["min"] = 'None'
        
        for record in self.table:
            for att in continuous_attribute_list:
                cur_value = record[att]
                if cur_value > self.attributes_info[att]["max"]:
                    self.attributes_info[att]["max"] = cur_value
                elif cur_value < self.attributes_info[att]["min"]:
                    self.attributes_info[att]["min"] = cur_value

        return self.attributes_info

    def discretizeAttribute(self, attribute, granularity):
        for i in range(0, len(self.attributes_info)):
            if self.attributes_info[i]["name"] == attribute:
                att_num = i
                i = len(self.attributes_info)
        max = self.attributes_info[att_num]["max"]
        min = self.attributes_info[att_num]["min"]

        distance = (max - min) / granularity
        intervals = []
        for i in range(0, granularity):
            start = distance * i + min
            end = start + distance
            dis_value_str = "[" + str(start) + ", " + str(end)
            if i == granularity - 1:
                dis_value_str += "]"
            else:
                dis_value_str += ")"
            intervals.append(dis_value_str)

        i = 0
        for record in self.table:
            cur_value = record[att_num]
            interval_num = int(granularity * (cur_value - min) / (max - min))
            if interval_num == granularity:
                interval_num = interval_num - 1
            dis_value = (intervals[interval_num],)
            update_record = record[0:att_num] + dis_value + record[att_num + 1 : ]
            self.table[i] = update_record
            i = i + 1
        
        return True

